/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frexpect_output.c                                  */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/12 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frexp_comm.h"
#include "frct_expect.h"

extern void frexpect_output(char **pp_result);
extern void __list_node_show(Frlist_node *p_st_node, char *p_outbuff, unsigned int ui_buff_size);
extern void __frexpect_node_free(Frlist_node* p_st_node);
extern Frhash_header st_hash_expect_ct;
extern void frhash2_free(Frhash_header *p_hash_hdr, list_node_free pf_free);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    //int i_ret = 0;
    Frexp *p_st_data=NULL;
    Frlist_node *p_st_node = NULL;
    //Frexp *p_node = NULL;
    char *arr_hash_key = NULL;
    
    arr_hash_key = (char *)frmalloc(30);
    memset(arr_hash_key, 0x00, 30);
    
    p_st_data = (Frexp *)frmalloc(sizeof(Frexp));
    memset(p_st_data, 0x00, sizeof(Frexp));
    p_st_data->us_proto = IPPROTO_TCP;
    p_st_data->ui_src_ip = iptools_parse_ipaddr("192.168.0.1");
    p_st_data->us_src_port = 80;
    p_st_data->ui_dst_ip = iptools_parse_ipaddr("192.168.1.1");
    p_st_data->us_dst_port = 90;
    p_st_data->master_src_ip = iptools_parse_ipaddr("192.168.2.1");
    p_st_data->master_dst_ip = iptools_parse_ipaddr("192.168.3.1");

    p_st_node = frlist_node_create(p_st_data);
    
    frhash2_create(&st_hash_expect_ct, 1);
    frhash2_addLong(&st_hash_expect_ct, 1, p_st_node);
    
    p_st_node->ui_key_len = frexpect_hash_key(p_st_data->us_proto, p_st_data->ui_src_ip, p_st_data->us_src_port, p_st_data->ui_dst_ip, p_st_data->us_dst_port, arr_hash_key, 30);
    p_st_node->p_key = arr_hash_key;
    
    frexpect_output(NULL);
    __frexpect_node_free(p_st_node);
    frfree(p_st_node);
    frfree(arr_hash_key);
    frfree(st_hash_expect_ct.p_list_array);
}

static void case2(void)
{
    //int i_ret = 0;
    Frexp *p_st_data=NULL;
    Frlist_node *p_st_node = NULL;
    //Frexp *p_node = NULL;
    char *arr_hash_key = NULL;
    char *p_test = NULL;
    
    arr_hash_key = (char *)frmalloc(30);
    memset(arr_hash_key, 0x00, 30);
    
    p_st_data = (Frexp *)frmalloc(sizeof(Frexp));
    memset(p_st_data, 0x00, sizeof(Frexp));
    p_st_data->us_proto = IPPROTO_TCP;
    p_st_data->ui_src_ip = iptools_parse_ipaddr("192.168.0.1");
    p_st_data->us_src_port = 80;
    p_st_data->ui_dst_ip = iptools_parse_ipaddr("192.168.1.1");
    p_st_data->us_dst_port = 90;
    p_st_data->master_src_ip = iptools_parse_ipaddr("192.168.2.1");
    p_st_data->master_dst_ip = iptools_parse_ipaddr("192.168.3.1");

    p_st_node = frlist_node_create(p_st_data);
    
    frhash2_create(&st_hash_expect_ct, 1);
    frhash2_addLong(&st_hash_expect_ct, 1, p_st_node);
    
    p_st_node->ui_key_len = frexpect_hash_key(p_st_data->us_proto, p_st_data->ui_src_ip, p_st_data->us_src_port, p_st_data->ui_dst_ip, p_st_data->us_dst_port, arr_hash_key, 30);
    p_st_node->p_key = arr_hash_key;
    
    frexpect_output(&p_test);
    __frexpect_node_free(p_st_node);
    frfree(p_st_node);
    frfree(arr_hash_key);
    frfree(p_test);
    frfree(st_hash_expect_ct.p_list_array);
}

int add_suite_frexpect_output(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("framework frexpect_output()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
